package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.entity.dto.course.CourseDTO;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author fantuan
 * @created 2025/10/8 21:18
 * @description
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageResult<Course> page(CourseDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<Course> result = courseMapper.selectByPage(dto);
        Page<Course> page = (Page<Course>) result;
        return PageResult.<Course>builder()
                .total((int) page.getTotal())
                .rows(result)
                .build();
    }

    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    @Override
    public void add(Course course) {
        boolean hasNull = BeanUtil.hasNullField(course,"id","description","createTime","updateTime");
        if(hasNull){
            throw new RuntimeException("参数错误");
        }
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }

    @Override
    public Course getCourseById(Integer id) {
        return courseMapper.getCourseById(id);
    }

    @Override
    public void update(Course course) {
        boolean hasNull = BeanUtil.hasNullField(course,"id","description","createTime","updateTime");
        if(hasNull){
            throw new RuntimeException("参数错误");
        }
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    @Override
    public List<Course> list() {
        return courseMapper.list();
    }

    @Override
    public List<Course> listBySubject(Integer subject) {

        return courseMapper.listBySubject(subject);
    }
}
